<?php
/*
This file is part of tribals-bot.

tribals-bot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

tribals-bot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with tribals-bot.  If not, see <http://www.gnu.org/licenses/>.
*/
/*
Questa funzione stampa ciò che si trova in mezzo di due stringhe.
La si lancia mettendo
str_middle_extract($StringaDaProcessare, $CaratteriPrimaDeiQualiConservare, $CaratteriDopoDeiQualiConservare);
viengono restituiti i caratteri in mezzo.
ESEMPIO:
<?php
$stringa = 'Ciao mondo, quanto sei bello!';
$prima = 'Ciao mondo, ';
$dopo = ' bello!';
echo(str_middle_extract($stringa, $prima, $dopo));
?>
Verrà stampato 'quanto sei' senza virgolette.
*/
function str_middle_extract($string, $str_before_extract, $str_after_extract){
    $str_start_pos = strpos($string, $str_before_extract);
    $str_end_pos = strpos($string, $str_after_extract);
    $num_char_to_extract = $str_end_pos - $str_start_pos;
    $extract = substr($string, $str_start_pos, $num_char_to_extract);
    $extract = str_replace($str_before_extract, '', $extract);
    return($extract);
}

/*
Questa funzione dorme per un tempo casuale fra un minimo e un massimo
*/
function casual_sleep($min=0, $max=60){
    sleep(rand($min, $max));
}


/*
Questa funzione estrae dati da una multipla riga e restituisce un array con il contentuo
*/
function multiple_line_str_extract($string, $str_before_extract, $str_after_extract, $after_l = 0){
    $i = 0;
    // Metto ogni riga in un array
    $strings = explode(PHP_EOL, $string);
    foreach($strings as $string){
        $i++;
        if($i >= $after_l){
            $result_t = str_middle_extract($string, $str_before_extract, $str_after_extract);
            if(isset($result_t) and !empty($result_t)){
                $result[] = $result_t;
            }
        }
    }
    if(!isset($result)){
        $result = array();
    }
    
    return $result;
}

?>
